from skillchallenge.models import *
import EventEngine
import CodeEngine

class LocationEngine: 
    
    INITIAL_MOVE = 'initial move'
    STAY = 'stay'
    MOVE = 'move'
    
    eventEngine = EventEngine.EventEngine()
    codeEngine = CodeEngine.CodeEngine()
    
    def create_location(self, name, description = '', initial = False):
        l = Location.objects.create(name = name, description = description,
                                initial = initial)
        
        action = self.codeEngine.create_message_action('you stayed in ' + 
                                                  l.name + '.')
        self.eventEngine.add_event(player_will = False, actions = [action],
                                   preconditions = [self.__is_in_query__(l)],
                                   info = self.__info_loc__(self.STAY, l),
                                   recurring = True, cost=0)
        if initial:
            self.initial_positions(l)
        return l
    
    def initial_positions(self, location):
#        print '<>>>> ' + str(location)
        precon = [self.codeEngine.create_negate_by_info(self.INITIAL_MOVE)]
        self.eventEngine.add_event(player_will = True, actions = self.__arrived_in_messages__(location),
                                   info = self.__info_loc__(self.INITIAL_MOVE, location), preconditions = precon, cost=0)
        
    def __info_loc__(self, info, *locations):
        return info + ":" + ":".join([str(l.id) for l in locations])
    
    def initial_moves(self):
        e = self.eventEngine.available_by_info(self.INITIAL_MOVE)
        return e
    
    def is_in_precon(self, location):
        return self.__is_in_query__(location)

    def player_moving(self):
        stay = 'Q(info__istartswith="' + self.STAY + '")'
        moving = 'Q(info__istartswith="' + self.MOVE + '")'
        initial_moving = 'Q(info__istartswith="' + self.INITIAL_MOVE + '")'
        is_in = '(' + stay + ' | ' + moving + ' | ' + initial_moving + ')&Q(timestamp__isnull = False)'
        return is_in

    def __is_in_query__(self, location):
        is_in = self.player_moving()
        here = 'Q(info__iendswith="'+str(location.id)+ '")'
        return self.codeEngine.create_latest_event_precon(is_in, here)
    
    def __arrived_in_messages__(self, location):
        return [self.codeEngine.create_message_action('you arrived in ' + \
                                                  location.name + '.'),
                self.codeEngine.create_message_action('you see: ' + \
                                                  location.description + '.')]
    
    def create_path(self, _from, _to, cost=1):
        self.eventEngine.add_event(player_will = True, actions = self.__arrived_in_messages__(_to),
                                   preconditions = [self.__is_in_query__(_from)], info = self.__info_loc__(self.MOVE, _from, _to),
                                   recurring = True, description = ('go to ' + _to.name + '.'),
                                   online = True, cost = cost)
    
    def move(self, _to, initial = False):
        if initial:
            info = self.INITIAL_MOVE
        else:
            info = self.MOVE
        
        for e in self.eventEngine.available_by_info(info):
            if e.info.endswith(str(_to.id)):
                return e